<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="108px"
    >
      <el-form-item label="疫苗名称：" prop="vaccinationName">
        <el-input
          v-model="queryParams.vaccinationName"
          @input="handleInput"
          placeholder="请输入园所名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-circle-close"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除
        </el-button>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="studentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="疫苗名称" align="center" prop="vaccinationName" />
      <el-table-column
        label="疫苗针数"
        align="center"
        prop="vaccinationCount"
      />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <!-- <el-form-item label="年级" prop="year">
          <el-select
            v-model="form.year"
            clearable
            filterable
            placeholder="请选择年级"
            :disabled="title == '修改疫苗情况' ? false : false"
            @change="getGradechange"
          >
            <el-option
              v-for="dict in gradeOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="班级" prop="classCode">
          <el-select
            v-model="form.classCode"
            clearable
            filterable
            @change="getStuchange"
            :disabled="title == '修改疫苗情况' ? false : false"
            placeholder="请选择班级"
          >
            <el-option
              v-for="item in classList"
              :key="item.value"
              :label="item.label"
              :value="parseInt(item.value)"
              size="small"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="姓名" prop="stuId">
          <el-select
            v-model="form.stuId"
            clearable
            filterable
            :disabled="title == '修改疫苗情况' ? false : false"
            placeholder="请选择姓名"
          >
            <el-option
              v-for="item in namelist"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="small"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="疫苗接种名称" prop="vaccinationName">
          <el-input
            v-model="form.vaccinationName"
            placeholder="请输入疫苗接种名称"
            clearable
            size="small"
            maxlength="25"
          />
        </el-form-item>
        <el-form-item label="疫苗针数" prop="vaccinationCount">
          <el-input
            v-model="form.vaccinationCount"
            placeholder="请输入疫苗针数"
            :disabled="title == '修改疫苗情况'? true : false"
            clearable
            size="small"
            maxlength="25"
          />
        </el-form-item>
        <!-- <el-form-item label="疫苗接种时间" prop="createTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.createTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择疫苗接种时间"
          >
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { selectName } from "@/api/base/device";
import { selectClazz } from "@/api/base/clazz";
import {
  baseVaccinationList,
  baseVaccinationUpdate,
  baseVaccinationInsert,
} from "@/api/vaccineManagement/index";
import {
  illnessList,
  addInsert,
  updateInsert,
  removeInsert,
} from "@/api/base/illmanager";

export default {
  name: "feedapply",
  dicts: ["year_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vaccinationName: "",
      },
      dateTime: "",
      classList: [],
      total: 0,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      form: {},
      namelist: [],
      // 表单校验
      rules: {
        year: [{ required: true, message: "不能为空", trigger: "blur" }],
        stuId: [{ required: true, message: "不能为空", trigger: "blur" }],
        classCode: [{ required: true, message: "不能为空", trigger: "blur" }],
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
        vaccinationName: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
      },
      isCurrentOptions: [],
      illnessProcessOptions: [],
      gradeOptions: [],
    };
  },
  created() {
    this.getDicts("year_type").then((response) => {
      this.gradeOptions = response.data;
    });
    this.getDicts("yes_or_no").then((response) => {
      this.isCurrentOptions = response.data;
    });
    //疾病处理方式字典
    this.getDicts("illness_process_mode").then((response) => {
      this.illnessProcessOptions = response.data;
    });
    //用户名
    this.getNamelist();
    //班级
    this.getQueryClazzList();

    this.getList();
  },
  methods: {
    handleInput(){

    },
    /** 查询班级列表 */
    async getQueryClazzList() {
      try {
        const obj = {
          schoolCode: this.queryParams.schoolCode,
          year: this.queryParams.year || this.form.year,
        };
        const { rows } = await selectClazz(obj);
        this.classList = rows.map((item) => {
          return {
            value: item.id,
            label: item.title,
          };
        });
      } catch (e) {
        console.log(e);
      }
    },
    /**查询用户列表*/
    async getNamelist() {
      try {
        const obj = {
          schoolCode: this.queryParams.schoolCode,
          year: this.queryParams.year || this.form.year,
          classId: this.queryParams.classCode || this.form.classCode,
        };
        const data = await selectName(obj);
        this.namelist = data.rows.map((item) => {
          return {
            label: item.name,
            value: item.id,
          };
        });
      } catch (e) {
        console.log(e);
      }
    },
    async getList() {
      try {
        this.loading = true;
        const data = await baseVaccinationList(this.queryParams);
        this.studentList = data.rows;
        this.total = data.total;
        this.loading = false;
      } catch (e) {
        console.log(e);
      }
    },
    getGradechange() {
      this.getQueryClazzList();
      this.getNamelist();
    },
    getStuchange() {
      this.getNamelist();
    },
    // 是否治愈当前字典翻译
    isCurrentFormat(row, column) {
      return this.selectDictLabel(this.isCurrentOptions, row.cureStatus);
    },
    // 年级字典翻译
    gradeTypeFormat(row, column) {
      return this.selectDictLabel(this.gradeOptions, row.year);
    },
    // 是否传染当前字典翻译
    isCurrentFormat1(row, column) {
      return this.selectDictLabel(this.isCurrentOptions, row.ifInfect);
    },
    // 疾病处理方式当前字典翻译
    illnessProcessFormat(row, column) {
      return this.selectDictLabel(this.illnessProcessOptions, row.method);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            baseVaccinationUpdate(this.form).then((response) => {
              if (response.code == 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            baseVaccinationInsert(this.form).then((response) => {
              if (response.code == 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除该条数据?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return removeInsert(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row);
      // baseVaccinationInsert
      this.reset();
      const id = row.id || this.ids;
      this.form = Object.assign({}, this.form, row);
      this.open = true;
      this.title = "修改疫苗情况";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加疫苗情况";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      console.log(this.ids);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        vaccinationName: "",
        pageNum: 1,
        pageSize: 10,
      };
      this.getGradechange();
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {
        vaccinationName: "",
        vaccinationCount: "",
      };
      this.resetForm("form");
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog .el-form-item__content {
  width: 67%;
}
</style>
